package model;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import utils.DateTest;


/**
 * Klasa predstavlja Air_Transport let.
 */
@Entity
@Table(name="flight")
@NamedQueries(
	{
		@NamedQuery(name = "all_flights", query = "select f from Flight f order by f.departureTime"),
		@NamedQuery(name = "flight_by_id", query = "select f from Flight f where f.id = :id")
	}
)
public class Flight implements Serializable {

	private static final long serialVersionUID = -3711061249159987807L;

	@Id
	@Column(name = "id")
	private String id;
	
	private Date departureTime;
	private Date arrivalTime;
	
	@OneToOne(cascade = {})
	private City source;
	
	@OneToOne(cascade = {})
	private City destination;
	
	private int reservedSeatsCount;
	
	private int maxSeatsCount;
	
	
//	@ManyToMany(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER, mappedBy = "flights")
	@ManyToMany(cascade = {}, fetch = FetchType.EAGER, mappedBy = "flights")
	private List<Passenger> passengers;
	
	
	public Flight() {}
	
	
	public Flight(String id, Date depTime, Date arrTime, City src, City dest, int msc) {
		passengers = new Vector<Passenger>();
		this.id = id;
		departureTime = depTime;
		arrivalTime = arrTime;
		source = src;
		destination = dest;
		maxSeatsCount = msc;
	}


	public String getId() {
		return id;
	}


	public void setId(String id) {
		this.id = id;
	}


	public Date getDepartureTime() {
		return departureTime;
	}


	public void setDepartureTime(Date departureTime) {
		this.departureTime = departureTime;
	}


	public Date getArrivalTime() {
		return arrivalTime;
	}


	public void setArrivalTime(Date arrivalTime) {
		this.arrivalTime = arrivalTime;
	}


	public City getSource() {
		return source;
	}


	public void setSource(City source) {
		this.source = source;
	}


	public City getDestination() {
		return destination;
	}


	public void setDestination(City destination) {
		this.destination = destination;
	}
	
	
	@Override
	public String toString() {
		return "Flight: " + id;
	}


	public List<Passenger> getPassengers() {
		return passengers;
	}


	public void setPassengers(List<Passenger> passengers) {
		this.passengers = passengers;
	}


	public int getReservedSeatsCount() {
		return reservedSeatsCount;
	}


	public void setReservedSeatsCount(int reservedSeatsCount) {
		this.reservedSeatsCount = reservedSeatsCount;
	}


	public int getMaxSeatsCount() {
		return maxSeatsCount;
	}


	public void setMaxSeatsCount(int maxSeatsCount) {
		this.maxSeatsCount = maxSeatsCount;
	}
	
	
	public String getDepartureTimeFormated() {
		SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy. HH:mm");
		return sdf.format(departureTime);
	}
	
	public String getArrivalTimeFormated() {
		SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy. HH:mm");
		return sdf.format(arrivalTime);
	}
	
	
	public boolean hasFreeSeats() {
		return reservedSeatsCount < maxSeatsCount;
	}
	
	public boolean getExpired() {
		return DateTest.testDate(departureTime);
	}
	
	public void reserveSeat(Passenger passenger) {
		passengers.add(passenger);
		reservedSeatsCount++;
	}
	
	public void unreserveSeat(Passenger passenger) {
		passengers.remove(passenger);
		reservedSeatsCount--;
	}
	
}
